package com.bookstore.utils;

import java.security.Security;
import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.sun.net.ssl.internal.ssl.Provider;

public class SendMail {

	private String from = "815040903@qq.com";
	private String user = "815040903";
	private String password = "vcjtheklvpnpbejc";

	public void sendMail(String receiver, String text, String title) {
		Properties properties = new Properties();
		properties.setProperty("mail.smtp.host", "smtp.qq.com");
		properties.put("mail.smtp.host", "smtp.qq.com");
		properties.put("mail.smtp.auth", "true");

		Security.addProvider(new Provider());
		properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		properties.setProperty("mail.smtp.port", "465");
		properties.setProperty("mail.smtp.socketFactory.port", "465");
		Session session = Session.getDefaultInstance(properties);
		session.setDebug(true);
		MimeMessage message = new MimeMessage(session);
		try {
			message.setFrom(new InternetAddress(from));
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(receiver));
			message.setSubject(title);
			Multipart multipart = new MimeMultipart();
			BodyPart contentPart = new MimeBodyPart();
			contentPart.setContent(text, "text/html;charset=utf-8");
			multipart.addBodyPart(contentPart);
			message.setContent(multipart);
			message.saveChanges();
			Transport transport = session.getTransport("smtp");
			transport.connect("smtp.qq.com", user, password);
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String sendRegistMail(String receiver, String userName) {
		String registerId = "" + Math.random() * Math.random();
		String text = "";
		String title = "";

		sendMail(receiver, text, title);
		return registerId;
	}

}
